package com.shell.customer.base.controller;

import com.shell.bee.base.annotation.Logging;
import com.shell.bee.base.utils.StringUtil;
import com.shell.bee.entity.mvc.RetMsg;
import com.shell.bee.persistence.model.PageLimit;
import com.shell.customer.accreditation.vo.AccreditationVo;
import com.shell.customer.base.service.CusModifyService;
import com.shell.customer.base.service.CusRecModService;
import com.shell.customer.base.service.CustomerService;
import com.shell.customer.base.vo.CusRecModVo;
import com.shell.customer.base.vo.CusRecVo;
import com.shell.customer.group.service.CusGrpService;
import com.shell.customer.po.CusGrp;
import com.shell.customer.po.CusInfoAprv;
import com.shell.customer.po.CusRec;
import com.shell.customer.po.CusRecMod;
import com.shell.feign.PubUserFeign;
import com.shell.framework.utils.CommonDataUtil;
import com.winchain.feign.TrdAcctFeign;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

@RestController
public class CustomerController {
	@Autowired
	CustomerService customerService;
	@Autowired
	PubUserFeign PubUserFeign;
	@Autowired
	TrdAcctFeign trdAcctFeign;
	@Autowired
	private CusModifyService cusModifyService;
	@Autowired
	private CusRecModService cusRecModService;
	@Autowired
	private CusGrpService cusGrpService;

	@RequestMapping
	@Logging(module = "��������", methods = "����������������")
	public List<CusRecVo> findCusForGrp(PageLimit limit, String grpId, String searchValue, String tagCode,
			String cusLvl) {
		return this.customerService.findCusForGrp(limit, grpId, searchValue, tagCode, cusLvl);
	}

	@RequestMapping
	@Logging(module = "��������", methods = "��������������������������")
	public List<CusRecVo> findAllCusForAdmin(PageLimit limit, String grpId, String searchValue) {
		return this.customerService.findAllCusForAdmin(limit, grpId, searchValue);
	}

	@RequestMapping
	@Logging(module = "��������", methods = "����������")
	public List<CusRecVo> findCus(PageLimit limit, String searchValue, String cusType, boolean add, String tagCode,
			String excludeTagCode) {
		String[] cusTypes = { cusType };
		if (StringUtil.emptyAndNull(excludeTagCode)) {
			return this.customerService.findAllCus(limit, searchValue, cusTypes, add, tagCode);
		}
		return this.customerService.findAllCus(limit, searchValue, cusTypes, add, tagCode, excludeTagCode);
	}

	@RequestMapping
	@Logging(module = "��������", methods = "������������������")
	public RetMsg saveCus(CusRecVo customercVo) {
		boolean isAdd = this.customerService.saveCus(customercVo);
		if (isAdd) {
			return new RetMsg("success", "����������");
		}
		return new RetMsg("success", "����������");
	}

	@RequestMapping
	@Logging(module = "��������", methods = "������������������")
	public RetMsg saveArtisanCus(CusRecVo customercVo) {
		customercVo.setArtisan(true);
		boolean isAdd = this.customerService.saveCus(customercVo);
		if (isAdd) {
			return new RetMsg("success", "����������");
		}
		return new RetMsg("success", "����������");
	}

	@RequestMapping
	@Logging(module = "��������", methods = "������������������")
	public RetMsg saveEntInfo(@RequestBody AccreditationVo accreditationVo) {
		this.cusModifyService.saveEntInfo(accreditationVo);
		return new RetMsg("success", "����������");
	}

	@RequestMapping
	@Logging(module = "��������", methods = "������������������������")
	public List<CusRecModVo> getAprvCus(PageLimit limit, CusRecModVo recBakVo) {
		String[] grpIdArr = null;
		String tagCodes = "";
		if ("2".equals(recBakVo.getCusState())) {
			tagCodes = "GROUP_FIRST_TRIAL";
		} else if ("3".equals(recBakVo.getCusState())) {
			tagCodes = "GROUP_LAST_TRIAL";
		}
		List<CusGrp> grps = this.cusGrpService.selectCusGroupByTagCode(CommonDataUtil.getCurEntId(), tagCodes);
		if (grps != null) {
			List<String> grpArr = new ArrayList();
			for (CusGrp grp : grps) {
				grpArr.add(grp.getGrpId());
			}
			grpIdArr = (String[]) grpArr.toArray(new String[grpArr.size()]);
		}
		recBakVo.setGrpIds(grpIdArr);
		return this.cusModifyService.getAprvCus(limit, recBakVo);
	}

	@RequestMapping
	@Logging(module = "��������", methods = "����������")
	public Map<String, String[]> getModItems(String cusId) {
		CusRecMod bak = (CusRecMod) this.cusRecModService.selectByKey(cusId);
		CusRec rec = new CusRec();
		BeanUtils.copyProperties(bak, rec);
		return this.cusModifyService.getModItems(rec);
	}

	@RequestMapping
	@Logging(module = "��������", methods = "��������")
	public RetMsg trialPass(CusInfoAprv aprv, String curState) {
		return this.cusModifyService.trialPass(aprv, curState);
	}

	@RequestMapping
	@Logging(module = "��������", methods = "��������")
	public RetMsg trialReject(CusInfoAprv aprv) {
		this.cusModifyService.trialReject(aprv);
		return new RetMsg("success", true, "��������������");
	}

	@RequestMapping
	@Logging(module = "��������", methods = "��������")
	public RetMsg finalPass(CusInfoAprv aprv, String curState) {
		return this.cusModifyService.finalPass(aprv, curState);
	}

	@RequestMapping
	@Logging(module = "��������", methods = "��������")
	public RetMsg finalReject(CusInfoAprv aprv) {
		this.cusModifyService.finalReject(aprv);
		return new RetMsg("success", true, "����������������");
	}

	@RequestMapping
	@ResponseBody
	@Logging(module = "��������", methods = "������������������������")
	public CusRecModVo selectEnterpriseInfo(String cusId) throws Exception {
		return this.cusModifyService.selectCusRecModVo(cusId);
	}

	@RequestMapping
	@ResponseBody
	@Logging(module = "��������", methods = "����������������")
	public List<CusRec> findCusByType(String cusType) {
		if (StringUtil.emptyAndNull(cusType)) {
			return null;
		}
		String[] cusTypeArr = cusType.split(",");

		Example example = new Example(CusRec.class);
		Example.Criteria criteria = example.createCriteria();
		List<Object> cusTypes = new ArrayList();
		for (String type : cusTypeArr) {
			cusTypes.add(type);
		}
		criteria.andIn("cusType", cusTypes);
		criteria.andEqualTo("cusState", "E");
		Object list = this.customerService.selectByExample(example);
		return (List<CusRec>) list;
	}

	@RequestMapping
	@Logging(module = "��������", methods = "��������")
	public List<CusRec> findFocalEnts(String cusType, String entId) {
		return this.customerService.selectFocalEnts(cusType, entId);
	}

	@RequestMapping
	@ResponseBody
	@Logging(module = "��������", methods = "����������������")
	public List<CusRec> findFocalEntsForGroup(String focalId) {
		return this.customerService.selectFocalEntsForGroup(focalId);
	}

	@RequestMapping
	@ResponseBody
	@Logging(module = "��������", methods = "����")
	public RetMsg frozen(String cusId) {
		return this.customerService.frozen(cusId);
	}

	@RequestMapping
	@ResponseBody
	@Logging(module = "��������", methods = "����")
	public RetMsg thaw(String cusId) {
		return this.customerService.thaw(cusId);
	}

	@RequestMapping
	@Logging(module = "��������", methods = "������������������")
	public List<CusRec> findCusByGroupIdAndTagCode(String grpId, String tagCodes) {
		return this.customerService.selectCusByGroupIdAndTagCode(grpId, tagCodes);
	}

	@RequestMapping
	@Logging(module = "��������", methods = "����app��������")
	public Map<String, String> getAppShareInfo() {
		return this.customerService.getAppShareInfo();
	}
}
